package pathways.marvin_test;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;

import chemaxon.formats.MolImporter;
import chemaxon.marvin.beans.MSketchPane;
import chemaxon.marvin.beans.MViewPane;
import chemaxon.struc.MolAtom;
import chemaxon.struc.MolBond;
import chemaxon.struc.Molecule;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author Jia An Chen
 * @version 1.0
 */

public class MarvinTester extends JApplet
                          implements ActionListener,
                                     MouseListener,
                                     PropertyChangeListener {
   Molecule mol;
   MolAtom atom1;
   MolAtom atom2;
   MolBond bond;

   MViewPane viewer;
   MSketchPane sketch;

   public void init() {
      Container contentPane = this.getContentPane();
/*
      mol = new Molecule();
      atom1 = new MolAtom(0, 0);
      atom2 = new MolAtom(9, 9);
      bond = new MolBond(atom1, atom2);
*/
/*      MSketchPane mSketchPane = new MSketchPane();

      mSketchPane.setParams("background=#cccccc\n");
      mSketchPane.setParams("molbg=#000000\n");
      mSketchPane.setParams("colorScheme=cpk\n");
      mSketchPane.setParams("extrabonds=arom,wedge\n");

      contentPane.add(mSketchPane);
*/
/*
      mol.add(atom1);
      mol.add(atom2);
      mol.add(bond);
*/
      InputStream inputFile = null;
      try {
        URL codeBase = this.getCodeBase();
        URL fileURL = new URL(codeBase.toString() + "pathway/marvin_test/mols-2d/caffeine.mol");
        inputFile = fileURL.openStream();
        System.out.println(fileURL);
      }
      catch (MalformedURLException mue) {
        System.out.println(mue);
      }
      catch (IOException ioe) {
        System.out.println(ioe);
      }



viewer = new MViewPane();
viewer.addPropertyChangeListener(this);
try {
MolImporter mi = new MolImporter(inputFile);
mol = mi.read();
viewer.setM(0, mol);
} catch(Exception ex) {
ex.printStackTrace();
}

JDialog dialog = new JDialog((Frame)null, "MarvinView Dialog", false);
// Keyboard events are received by the dialog window but
dialog.addKeyListener(viewer); // processed by the bean
dialog.getContentPane().add(viewer);
dialog.pack();
dialog.setVisible(true);

contentPane.setLayout(new FlowLayout());
JButton button = new JButton("set color");
button.addActionListener(this);
contentPane.add(button);

JButton button2 = new JButton("print coords");
button2.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
//      viewer.applyRotationMatrices();
      Molecule[][] mArray;
      MolAtom[] atoms = null;

      mArray = viewer.getMols();
      for (int i = 0; i < mArray.length; i++) {
        for (int j = 0; j < mArray[i].length; j++) {
          System.out.println(mArray[i][j].getLocation());
          atoms = new MolAtom[mArray[i][j].getAtomCount()];
          for (int z = 0; z < atoms.length; z++) {
            atoms[z] = mArray[i][j].getAtom(z);
            System.out.println(atoms[z].getLocation());
          }
        }
      }
  }
});
contentPane.add(button2);

sketch = new MSketchPane();
sketch.setParams("viewonly=true\n"+
"implicitH=hetero\n");
//sketch.addPropertyChangeListener(this);
sketch.setMol(mol);

JDialog dialogSketch = new JDialog((Frame)null, "MarvinSketch Dialog", false);
// Keyboard events are received by the dialog window but
dialogSketch.addKeyListener(sketch); // processed by the bean
dialogSketch.getContentPane().add(sketch);
dialogSketch.pack();
dialogSketch.setVisible(true);


    }




    public void actionPerformed(ActionEvent ae) {

      System.out.println("button clicked");
      viewer.setColorScheme("atomset");

      Molecule[][] mArray;
      MolAtom[] atoms = null;

      mArray = viewer.getMols();
      for (int i = 0; i < mArray.length; i++) {
        for (int j = 0; j < mArray[i].length; j++) {
//          mArray[i][j].setSetSeqs(1);
          System.out.println("_" + mArray[i][j].toString() + "_");
          System.out.println("count:" + mArray[i][j].getAtomCount());
          atoms = new MolAtom[mArray[i][j].getAtomCount()];
          for (int z = 0; z < atoms.length; z++) {
            atoms[z] = mArray[i][j].getAtom(z);
            System.out.println(atoms[z].getSetSeq());
            atoms[z].setSetSeq(0);
          }
        }
      }
atoms[0].setSetSeq(2);

viewer.repaint();
//      viewer.setInvisibleSets(0);
//      viewer.setSetColor(1, Color.cyan);
System.out.println("getAtomsize():"+viewer.getAtomsize());
System.out.println("getBackground():"+viewer.getBackground());
System.out.println("getBallRadius():"+viewer.getBallRadius());
System.out.println("getColorScheme():"+viewer.getColorScheme());
System.out.println("getDebug():"+viewer.getDebug());
System.out.println("getDispopts():"+viewer.getDispopts());
System.out.println("getDownWedge():"+viewer.getDownWedge());
System.out.println("getExplicitH():"+viewer.getExplicitH());
System.out.println("getFile():"+viewer.getFile());
System.out.println("getFormat():"+viewer.getFormat());
System.out.println("getImplicitH():"+viewer.getImplicitH());
System.out.println("getIniFile():"+viewer.getIniFile());
System.out.println("getIniProps():"+viewer.getIniProps());
System.out.println("getInvisibleSets():"+viewer.getInvisibleSets());
System.out.println("getLabels():"+viewer.getLabels());
System.out.println("getMolbg():"+viewer.getMolbg());
//System.out.println(viewer.getParameter(java.lang.String key)
//System.out.println(viewer.getParams()
System.out.println("getRendering():"+viewer.getRendering());
System.out.println("getSetColor(0):"+viewer.getSetColor(63));
System.out.println("getStickThickness():"+viewer.getStickThickness());
System.out.println("isSaveIniEnabled():"+viewer.isSaveIniEnabled());
System.out.println("loadIni():"+viewer.loadIni());
System.out.println("--------------------------------------");
System.out.println("getAnimDelay():"+viewer.getAnimDelay());
System.out.println("getAnimFPS():"+viewer.getAnimFPS());
System.out.println("getAutoTabScale():"+viewer.getAutoTabScale());
System.out.println("getBestTabScale():"+viewer.getBestTabScale());
System.out.println("getBestTabScale(0):"+viewer.getBestTabScale(0));
System.out.println("getBorderWidth():"+viewer.getBorderWidth());
//System.out.println("getC():"+viewer.getC(1));
System.out.println("getCellCount():"+viewer.getCellCount());
System.out.println("getEditable():"+viewer.getEditable());
System.out.println("getIniFile():"+viewer.getIniFile());
//System.out.println("getL():"+viewer.getL(0));
System.out.println("getM():"+viewer.getM(0));
//System.out.println("getM"+viewer.getM(int n, java.lang.String fmt));
System.out.println("getNavmode():"+viewer.getNavmode());
System.out.println("getSelectedIndex():"+viewer.getSelectedIndex());
//System.out.println("getT():"+viewer.getT(0));
System.out.println("getTabScale():"+viewer.getTabScale());
System.out.println("getTabScale(0):"+viewer.getTabScale(0));
System.out.println("getWinScale():"+viewer.getWinScale());
System.out.println("isAnimSync():"+viewer.isAnimSync());
System.out.println("isSelectable():"+viewer.isSelectable());


System.out.println(viewer.getM(0, "mol"));

System.out.println(viewer.getParameter("dispQuality\n"));
viewer.setParams("rows=1\n"+
"cols=1\n"+
"layout0=:2:2:M:1:0:1:1:c:b:1:1:L:0:0:1:1:c:n:0:1\n"+
"param0=:M:100:100:L:10b\n"+
"selectable=false\n"+
"selection0=0,1\n"+
"\n"+
"# MarvinSketch properties also have to be specified here for the\n"+
"# sketcher windows that can be launched using Edit/Structure.\n"+
"extrabonds=arom,wedge\n");
viewer.setM(0,mol);
    }

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered");
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
System.out.println(viewer.getSelectedIndex());
}



public void propertyChange(PropertyChangeEvent ev) {
    System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
    String name = ev.getPropertyName();
    if(name.equals("objectAtPointer")) {
	Object o = ev.getNewValue();
        if(o != null && o instanceof MolAtom)
	    o = (MolAtom)o;
            System.out.println(o);
    }
}


/*
   public void paint(Graphics g) {
      Rectangle rect = this.getBounds();
      MolPrinter printer = new MolPrinter();
      printer.setM(mol);


printer.setAtomsize(7);
printer.setBondWidth(7);
printer.setMag(7);

System.out.println(printer.getAtomsize());
System.out.println(printer.getBondWidth());
System.out.println(printer.getDispopts());
System.out.println(printer.getMag());


      printer.paint(g, rect);
   }*/
}